<?php


namespace App\Http\Controllers;

use Illuminate\Http\Request;
//use App\Http\Requests;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
class UsersController extends Controller
{

    //构造函数 __construct(){}的引入以及对中间件的使用[修]
    public function __construct()
    {
         //调用中间件方法：参数auth和except
         $this->middleware("auth",[
             'except'=>['show','create','store','index','confirmEmail'],
         ]);
         //guest
         $this->middleware('guest', [
               'only' => ['create'],
         ]);

    }
    //展示所有用户
    public function index()
    {
       $users=User::paginate(8);
       return view('users.index',compact('users'));
    }
    //注册
    public function create()
    {

      return view('users.create');
    }

    /**1 show方法页面
       2 传值 由 use App\Http\Requests 接收判断是什么传值
       3 引入 use App\Models\User  数据库信息相关的查询操作
    **/

    public function show(User $user)
    {
      $statuses=$user->statuses()
                     ->orderBy('created_at','desc')
                     ->paginate(10);

     return view('users.show',compact('user','statuses'));
    }
   /***验证并保存数据到数据库
   1.validate 启用laravel 自带的validate 验证数据

   ***/
  public function store(Request $request)
  {
       //验证数据
      $this->validate($request,[
         'name'=> 'required|min:3|max:20',
         'email'=>'required|email|unique:users|max:25|distinct',
         'password'=>'required|confirmed|min:3|max:6',
       ]);
      $user=User::create([
        'name'=>$request->name,
        'email'=>$request->email,
        'password'=>bcrypt($request->password),
      ]);

     $this->sendEmailConfirmationTo($user);//发送邮件认证 所传数据为$user
     session()->flash('success', '验证邮件已发送到你的注册邮箱上，请注意查收。');
     return redirect('/');
  }
  //发送邮件的模板
  public function sendEmailConfirmationTo($user)
  {
     $view='emails.confirm';//准备发送的模板
     $data=compact('user');
     $from="1260127789@qq.com";
     $name="wen3742";
     $to=$user->email;//单用户调用邮件
     $subject="感谢注册 Sample 应用！请确认你的邮箱。";

     Mail::send($view,$data, function($message) use ($from,$name,$to,$subject) {
          $message->from($from,$name)->to($to)->subject($subject);
     });
  }

  /*
  **用户个人修改页面
  ***/
  public function edit(User $user)
  {

    $this->authorize('update', $user);
    return  view("users.edit",compact('user'));
  }

  //用户进行更新操作
  public function update(User $user,Request $request)
  {
    //验证数据
      $this->validate($request,[
        'name'=> 'required|min:3|max:20',
        'password'=>'nullable|confirmed|min:5|max:20',
      ]);

      $this->authorize('update', $user);

      $data=[];
      $data['name']=$request->name;
      //优化：修改密码或不修改密码
      if($request->password){
        $data['password']=bcrypt($request->password);
      }
      //执行更新
      $user->update($data);
   //提示信息
   session()->flash('success','修改成功');
   //跳转到个人主页
   return redirect()->route('users.show',$user->id);
  }
  //删除用户管理员特有
  public function destroy(User $user)
  {
     $this->authorize('destroy',$user);//权限
     $user->delete();
     session()->flash("success",'删除成功');
     return back();
  }
  //模拟邮件发送接收

  public function confirmEmail($token)
  {
     $user=User::where("activation_token",$token)->firstOrFail();

     $user->activated=true;
     $user->activation_token=null;
     $user->save();
     Auth::login($user);
     session()->flash('success','恭喜，激活成功');
     return redirect()->route('users.show',[$user]);
  }
  //显示关注的人
  public function followings(User $user)
{
    $users = $user->followings()->paginate(30);
    $title = '关注的人';
    return view('users.show_follow', compact('users', 'title'));
}
//显示粉丝数
public function followers(User $user)
{
    $users = $user->followers()->paginate(30);
    $title = '粉丝';
    return view('users.show_follow', compact('users', 'title'));
}


}
